package com.itheima.arithmetic.stack;

import java.util.Scanner;

/**
 * 括号匹配 {[]<>[[}
 * 栈数据结构实现
 */
public class KuoHaoStack {

    /**
     * @param s 待匹配的括号串 {[]<>[[}
     */
    public static boolean isMatch(String s){
        MyStack<Character> myStack = new ArrayStack<>(20);
        Character top;
        for (char c : s.toCharArray()) {
            switch (c) {
                case '(':
                case '{':
                case '[':
                    // 左括号入栈
                    myStack.push(c);
                    break;

                    // 右括号匹配, 左括号出栈
                case ')':
                    top = myStack.pop();
                    if (top == null) {
                        return false;
                    }
                    if ('(' == top) {
                        break;
                    } else {
                        return false;
                    }

                case '}':
                    top = myStack.pop();
                    if (top == null) {
                        return false;
                    }
                    if ('{' == top) {
                        break;
                    } else {
                        return false;
                    }

                case ']':
                    top = myStack.pop();
                    if (top == null) {
                        return false;
                    }
                    if (']' == top) {
                        break;
                    } else {
                        return false;
                    }

            }
        }

        // 空说明括号匹配
        return myStack.isEmpty();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String next = scanner.next();
            boolean match = isMatch(next);
            System.out.println("当前是否匹配: " + match);
        }
    }
}
